package com.hnichr.ump.talent.library.provider.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hnichr.ump.common.talent.library.entity.WorkInfo;
import com.hnichr.ump.common.talent.library.service.WorkInfoService;
import com.hnichr.ump.talent.library.provider.SyncPolicyProvider;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Component
public class WorkInfoSyncPolicyProvider implements SyncPolicyProvider {

    private static final String KEY = "workInfoList";

    @Autowired
    private WorkInfoService workInfoService;

    @Override
    public boolean support(JSONObject jsonObject) {
        return jsonObject.containsKey(KEY);
    }

    @Override
    @Transactional
    public void sync(String syncDate, JSONObject jsonObject) {
        JSONArray workInfoList = jsonObject.getJSONArray(KEY);
        if(CollectionUtils.isEmpty(workInfoList)){
            return;
        }
        for(Object workInfoObject:workInfoList){
            if(workInfoObject instanceof JSONObject){
                JSONObject workInfoJSONObject = (JSONObject) workInfoObject;
                WorkInfo workInfo = new WorkInfo();
                workInfo.setId(workInfoJSONObject.getString("id"));
                workInfo.setDelFlag(workInfoJSONObject.getBoolean("delFlag"));
                workInfo.setUserId(workInfoJSONObject.getString("userId"));
                if(workInfoJSONObject.containsKey("createTime")){
                    workInfo.setOrginalCreateTime(new Date(workInfoJSONObject.getLong("createTime")));
                }
                if(workInfoJSONObject.containsKey("updateTime")){
                    workInfo.setOrginalUpdateTime(new Date(workInfoJSONObject.getLong("updateTime")));
                }

                workInfo.setCompanyName(workInfoJSONObject.getString("companyName"));
                workInfo.setCompanyType(workInfoJSONObject.getString("r1"));
                workInfo.setDepart(workInfoJSONObject.getString("r2"));
                workInfo.setCompanyScale(workInfoJSONObject.getString("r3"));
                workInfo.setSuperior(workInfoJSONObject.getString("r4"));
                workInfo.setSubordinates(workInfoJSONObject.getString("r5"));
                workInfo.setLeavingReason(workInfoJSONObject.getString("r6"));
                workInfo.setPositionName(workInfoJSONObject.getString("positionName"));
                workInfo.setPositionType(workInfoJSONObject.getString("positionType"));
                if(workInfoJSONObject.containsKey("workStartTime")){
                    workInfo.setStartTime(new Date(workInfoJSONObject.getLong("workStartTime")));
                }
                if(workInfoJSONObject.containsKey("workEndTime")){
                    workInfo.setEndTime(new Date(workInfoJSONObject.getLong("workEndTime")));
                }
                workInfo.setSalary(workInfoJSONObject.getInteger("salary"));
                workInfo.setWorkDescribe(workInfoJSONObject.getString("workDescribe"));
                workInfo.setWorkAchievement(workInfoJSONObject.getString("workAchievement"));
                workInfo.setWorkContent(workInfoJSONObject.getString("workContent"));
                workInfo.setResumeId(workInfoJSONObject.getString("resumeId"));
                workInfo.setSyncDate(syncDate);
                workInfoService.merge(workInfo);
            }
        }
    }
}
